<template>
  <div style="overflow-x: auto">
    <table cellspacing="0" class="table">
      <thead>
        <tr>
          <th v-for="item in listData" :key="item.id" :width="item.width">
              <span class="ml">{{item.field_name}}</span> 
          </th>
        </tr>
      </thead>
      <tbody >
        <tr v-for="(item, index) of list" :key="index">
          <td style="font-size: 30px; text-align: center; width: 100px">
            <Icon v-if="JSON.stringify(item) !== '[]' || JSON.stringify(item) !== '{}'"   @click="onAdd(item)" type="md-add" />
            <Icon v-if="JSON.stringify(item) !== '[]' || JSON.stringify(item) !== '{}'" @click="onDelete(item, index)"  type="ios-trash-outline" />
          </td>
          <td v-for="(todo, index1) in item" :key="index1">
            <Input type="textarea"   style="width:200px;"   v-model="item[index1]"/>
          </td>
        </tr>
      </tbody>
    </table>
    <Loader  :status="status" @refresh="getData"></Loader>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import {getMinValue, getMaxValue, getSum, onSet} from "@/views/sales/common/TableSaleMethods"
export default {
  components: {
  },
  props: {
    list: "",
    status: "",
    listData: "",
    type: '',
    target: '',
    ctype: false
  },
  data() {
    return {
      selectAll: false,
      totalItem: 1,
      pageSize: window.$pageSize,
      currentPage: 1,
      viewData: [],
      id: this.$route.query.id || ''
    };
  },
  created () {
    this.initData()
  },
  methods: {
    onAdd(item) {
      this.list.push(JSON.parse(JSON.stringify(this.target)))
      console.log(this.list)
    },
    onDelete(item, index) {
      if (this.list.length <= 1) {
        return
      }
      this.list.splice(index, 1)
    },
    initData () {
      // this.totalItem = this.list.length;
      this.viewData =  this.list;
    },
    getSum(param, object) {
      return getSum(param, object)
    },
    getMinMaxValue(param, object) {
      if(getMinValue(param, object)==getMaxValue(param, object)) {
        return `[${getMinValue(param, object)}]`
      }else {
        return  `[${getMinValue(param, object)}-${getMaxValue(param, object)}]`;
      }
    },
    onSelectAll() {
      // 全选 、全不选
      this.$emit("onChangeSelectAll", this.selectAll);
    },
    onJoinSale(child, item) {
      this.$emit("onJoinSale", child, item);
    },
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    onJoinSaleCheck(child, item) {
      this.$emit("onJoinSaleCheck", child, item);
    },
    onDeleteSale(index, data, index1, data1) {
      this.$emit("onDeleteSale", index, data, index1, data1);
    },
    onPageChange(val) {
      this.viewData =  this.list.slice(((val-1) * this.pageSize),val * this.pageSize);
    },
    onPageSizeChange(val) {
      this.pageSize= val;
      this.viewData = this.list.slice(0,val)
    },
    onReplenish(item) {
      this.$refs.replenish.reset(item)
    },
    // 商品积分
    onSetgiftPoint(item, child) {
      if((!item['giftPoint'] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item['giftPoint'])) && item.productSkuList) {
        return 
      }
      this.onNegative(item, 'giftPoint')
      // if (!item.giftPoint) return
      let ids = []
      if (child) {
        ids = [item.acId]
      } else {
        ids = item.productSkuList.map(item=> {
          return item.acId
        })
      }
      this.$store.dispatch('setGiftPoint', {activityId: this.id, ids: ids, giftPoint: item.giftPoint}).then(data => {
        this.$Notice.success({
          title: '温馨提示',
          desc: "商品积分修改成功"
        })
        onSet(item, child, 'giftPoint')
      })
    },
    // 商品返利
    onSetGiftRebate(item, child) {
      if((!item['giftRebate'] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item['giftRebate'])) && item.productSkuList) {
        return 
      }
      this.onNegative(item, 'giftRebate')
      // if (!item.giftRebate) return
      let ids = []
      if (child) {
        ids = [item.acId]
      } else {
        ids = item.productSkuList.map(item=> {
          return item.acId
        })
      }
      this.$store.dispatch('setGiftRebate', {activityId: this.id, ids: ids, giftRebate: item.giftRebate}).then(data => {
        this.$Notice.success({
          title: '温馨提示',
          desc: "商品返利修改成功"
        })
        onSet(item, child, 'giftRebate')
      })
    },
    // 促销库存
    onSetsellOutQuantity(item, child) {
      // if (!item.sellOutQuantity) return
      if((!item['sellOutQuantity'] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item['sellOutQuantity'])) && item.productSkuList) {
        return 
      }
      let ids = []
      if (child) {
        ids = [item.acId]
      } else {
        ids = item.productSkuList.map(item=> {
          return item.acId
        })
      }
      this.$store.dispatch('setSellOutQuantity', {activityId: this.id, ids: ids, sellOutQuantity: item.sellOutQuantity}).then(data => {
        this.$Notice.success({
          title: '温馨提示',
          desc: "促销库存修改成功"
        })
        onSet(item, child, 'sellOutQuantity')
      })
    },
    onSetWarningQuantity(item, child) {
      onSet(item, child, 'warningQuantity')
    },
    // 促销价
    onSetSale(item, child) {
      // if (!item.sale) return
      if((!item['sale'] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(item['sale'])) && item.productSkuList) {
        return 
      }
      this.onNegative(item, 'sale')
      let ids = []
      if (child) {
        ids = [item.acId]
      } else {
        ids = item.productSkuList.map(item=> {
          return item.acId
        })
      }
      this.$store.dispatch('setProQuantity', {activityId: this.id, ids: ids, promotionPrice : item.sale}).then(res => {
        this.$Notice.success({
          title: '温馨提示',
          desc: "促销价修改成功"
        })
        onSet(item, child, 'sale')
      })
    },
    // 修改优惠券
    onSetgiftCouponId(data) {
      if (!data.giftCouponId) return
      let ids = []
      if (data.productSkuList && data.productSkuList.length) {
        ids = data.productSkuList.map(item=> {
          return item.acId
        })
      } else {
        ids = [data.acId]
      }
      this.$store.dispatch('setCouponId', {activityId: this.id, ids: ids, giftCouponId: data.giftCouponId == '0' ? '' : data.giftCouponId}).then(res => {
        this.$Notice.success({
          title: '温馨提示',
          desc: "优惠券修改成功"
        })
        if (data.productSkuList && data.productSkuList.length) {
          for (let item of data.productSkuList) {
            item['giftCouponId'] = data['giftCouponId']
          }
        }
      })
    },
    onBatchSet(param) {
      let type = ''
      switch(param.setType) {
        case '0':
          type = 'integral';
        break;
        case '1':
          type = 'rebate'
        break;
        case '2':
          type = ''
        break;
        case '3':
          type = 'sellOutQuantity'
        break;
        case '4':
          type = 'sale'
        break;
      }
      if(type) {
        this.$refs.editIntegral.reset({setType: type, type: param.type})
      } else {
        this.$refs.editCoupon.reset({type: this.type})
      }
    },
    onTestValue(row, type) {
      if((!row[type] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(row[type])) && row.productSkuList) {
        return false;
      }
      let value = JSON.parse(JSON.stringify(row))
      if(!(/^[1-9]\d*$/.test(row[type]))) {
        for (let i=0;  i<row[type].length ;i++) {
          if(!(/^[1-9]\d*$/.test(row[type][i]))) {
            row[type] = row[type].substring(0, i) + row[type].substring(i+1, row[type].length)
          }
        }
      }
    },
    onNegative(row, type) {
      if((!row[type] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(row[type])) && row.productSkuList) {
        return 
      }
      let value = JSON.parse(JSON.stringify(row))
      if(!(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)|(0))$/.test(row[type]))) {
        for (let i=0;  i<row[type].length; i++) {
          if(!(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)|(0))$/.test(row[type][i]))) {
            row[type] = row[type].substring(0, i) + row[type].substring(i+1, row[type].length)
          }
        }
      }
    }
  },
  computed: {
    ...mapGetters([
      'bargainCouponsData'
    ])
  },
  filters: {
    status(val) {
      if (val) {
        return "上架";
      } else {
        return "下架";
      }
    }
  },
  watch: {
    list () {
      this.initData()
    }
  }
};
</script>

